package cn.chaos.bio01;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

public class InetAddressExample {

    public static void main(String[] args) {
        
        try {
            Enumeration<NetworkInterface> ifList = NetworkInterface.getNetworkInterfaces();
            
            if (ifList == null){
                System.out.println("no interface found");
            } else {
                while (ifList.hasMoreElements()) {
                    NetworkInterface ni = ifList.nextElement();
                    System.out.println("Interface :"+ ni.getName() + "-" + ni.getDisplayName());
                    
                    Enumeration<InetAddress> addList = ni.getInetAddresses();
                    
                    if ( addList != null) {
                        while (addList.hasMoreElements()) {
                            InetAddress address = addList.nextElement();
                            System.out.println("\taddress " +
                                    ((address instanceof Inet4Address) ? "v4" :
                                        (address instanceof Inet6Address) ? "v6" : "??"));
                            System.out.println(" : "+ address.getHostAddress() + " = " + address.getHostName());
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        
        System.out.println(" -- ");
        
        List<String> hostNames = Arrays.asList("localhost", "DESKTOP-ITIEPCM", "192.168.88.1", "wokao", "10.0.0.9");
        
        for (String hostName : hostNames ){
            try {
                InetAddress[] addressList = InetAddress.getAllByName(hostName);
                for (InetAddress add : addressList) {
                    System.out.println("\t"+ add.getHostName() +" / " +add.getHostAddress());
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

    }
}
